package com.atguigu.tms.mock.service.impl;

import com.atguigu.mock.util.RandomBox;
import com.atguigu.tms.mock.bean.BaseComplex;
import com.atguigu.tms.mock.bean.BaseOrgan;
import com.atguigu.tms.mock.bean.ExpressCourier;
import com.atguigu.tms.mock.bean.ExpressCourierComplex;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.mapper.ExpressCourierComplexMapper;
import com.atguigu.tms.mock.service.BaseComplexService;
import com.atguigu.tms.mock.service.BaseOrganService;
import com.atguigu.tms.mock.service.ExpressCourierComplexService;
import com.atguigu.tms.mock.service.ExpressCourierService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-14
 */
@Service
public class ExpressCourierComplexServiceImpl extends AdvServiceImpl<ExpressCourierComplexMapper, ExpressCourierComplex> implements ExpressCourierComplexService {

    @Autowired
    BaseComplexService baseComplexService;

    @Autowired
    BaseOrganService baseOrganService;

    @Autowired
    ExpressCourierService expressCourierService;

    public  void initExpressCourierComplex(List<ExpressCourier> expressCourierList){

        List<ExpressCourierComplex> expressCourierComplexList=new ArrayList<>(expressCourierList.size());

        List<BaseComplex> baseComplexList = baseComplexService.list(new QueryWrapper<BaseComplex>().orderByAsc("district_id"));
        RandomBox randomBox = new RandomBox(3, 7);
        int courierIdx=0;
        Long districtId=0L;

        List<ExpressCourier> courierInComplexList=new ArrayList<>();
        for (int i = 0; i < baseComplexList.size(); i++) {
            BaseComplex baseComplex = baseComplexList.get(i);

            ExpressCourier expressCourier   = expressCourierList.get(courierIdx);


            ExpressCourierComplex expressCourierComplex = new ExpressCourierComplex();
            expressCourierComplex.setComplexId(baseComplex.getId());
             districtId = baseComplex.getDistrictId();

            BaseOrgan baseOrgan = baseOrganService.getOrganByRegionId(districtId);
            expressCourier.setOrgId(baseOrgan.getId());
            courierInComplexList.add(expressCourier);

            expressCourierComplex.setCourierEmpId(expressCourier.getEmpId());
            expressCourierComplex.setCreateTime(AppConfig.mock_date);
            expressCourierComplex.setIsDeleted("0");

            expressCourierComplexList.add(expressCourierComplex);



            if(expressCourier.getOrgId()!=null) {  //如果没接过小区
                if (!randomBox.getRandBoolValue()) { //该快递员不再接小区  否则可以继续接多个小区
                    courierIdx++;
                } else {
                    if ((i + 1)< baseComplexList.size() ) {
                        BaseComplex nextBaseComplex = baseComplexList.get(i + 1);
                        Long nextComplexDistrictId = nextBaseComplex.getDistrictId();
                        if (!districtId.equals(nextComplexDistrictId) ) {
                            courierIdx++;   //如果连续接多个小区 则不能跨机构
                        }
                    }

                }
            }
        }

        this.saveOrUpdateBatch(expressCourierComplexList,1000,true);
        expressCourierService.saveOrUpdateBatch(courierInComplexList,1000,true);

    }



}
